package moduleTest;

import java.util.Collection;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;


@Stateless
public class BookFacadeEJB implements BookFacade {
	@PersistenceContext(unitName = "POLEINFODB")
	protected EntityManager entityMgr;
	
	public Book addBook(Book b) {
		entityMgr.persist(b);
		return b;
	}

	public void clearAllBooks() {
		Collection<Book> books = listBooks();
		for(Book nextBook : books){
			entityMgr.remove(nextBook);
		}
	}

	@SuppressWarnings("unchecked")
	public Collection<Book> listBooks() {
		 return entityMgr.createQuery("SELECT b FROM Book as b").getResultList();
	}
	
	public Book updateBookSummary(String title,String author,String summary){
		Book b = entityMgr.find(Book.class, new BookPK(title,author));
		b.setSummary(summary);
		return b;
	}
}
